﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommonData;
using CommonData.CommonItems;

namespace DataClient
{
    public partial class PasswordConfigForm : CommonData.CommonChildForm
    {
        string _STR_OLD_PASS = string.Empty;
        public PasswordConfigForm()
        {
            InitializeComponent();

            _STR_OLD_PASS = CommonFunctions.DecodeFrom64(DataClient.Properties.Settings.Default.PasswordSettingDB);
            ValueListClass clValueList = new ValueListClass();
            DataTable tblDBConfig = clValueList.GetDatabaseConfigList();
            iluSConfig.Properties.DataSource = tblDBConfig;
            iluDConfig.Properties.DataSource = tblDBConfig;

            iluSConfig.EditValue = DataClient.Properties.Settings.Default.SConfigValue;
            iluDConfig.EditValue = DataClient.Properties.Settings.Default.DConfigValue;
        }

        private void btnChangePass_Click(object sender, EventArgs e)
        {
            ChangePasswordForm frmChangePass = new ChangePasswordForm();
            frmChangePass.ShowDialog();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DataClient.Properties.Settings.Default.SConfigValue = Convert.ToInt32(iluSConfig.EditValue);
            DataClient.Properties.Settings.Default.DConfigValue = Convert.ToInt32(iluDConfig.EditValue);
            DataClient.Properties.Settings.Default.Save();
            DataClient.Properties.Settings.Default.Reload();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text.Trim().Equals(_STR_OLD_PASS))
            {
                MessageClass.ShowInformationMessage("Mật khẩu hợp lệ. Bạn có thể thay đổi cấu hình dữ liệu.");
                iluSConfig.Properties.ReadOnly = false;
                iluDConfig.Properties.ReadOnly = false;
            }
            else
            {
                MessageClass.ShowErrorMessage("Mật khẩu không đúng. Hãy thử lại");
            }
        }
    }
}
